package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.SysRoleMenu;
import com.farm.kaiping.po.SysRoleMenuPO;
import com.farm.kaiping.mapper.SysRoleMenuMapper;
import com.farm.kaiping.service.SysRoleMenuService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 角色菜单表(SysRoleMenu)表服务实现类
 *
 * @author zhuhui
 * @since 2024-11-26 22:58:21
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysRoleMenuServiceImpl extends ServiceImpl<SysRoleMenuMapper, SysRoleMenu> implements SysRoleMenuService {
    
	private LambdaQueryWrapper<SysRoleMenu> generateQueryWrapper(SysRoleMenuPO sysRoleMenuPO){
		LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
		// 角色ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRoleMenuPO.getRoleId()), SysRoleMenu::getRoleId, sysRoleMenuPO.getRoleId());
		// 菜单ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRoleMenuPO.getMenuId()), SysRoleMenu::getMenuId, sysRoleMenuPO.getMenuId());
		return queryWrapper;
	}
	
	/**
     * @describe 查询角色菜单表list
     * @author  zhuhui
     * @date  2024-11-26 22:58:21
     */
	@Override
	public List<SysRoleMenu> getSysRoleMenuList(SysRoleMenuPO sysRoleMenuPO){
		LambdaQueryWrapper<SysRoleMenu> queryWrapper = this.generateQueryWrapper(sysRoleMenuPO);
        return this.baseMapper.selectList(queryWrapper);
	}

    /**
     * @describe 新增角色菜单表
     * @author  zhuhui
     * @date  2024-11-26 22:58:21
     */
	@Override
	public String saveSysRoleMenu(SysRoleMenuPO sysRoleMenuPO) {
		if (ObjectUtil.isEmpty(sysRoleMenuPO.getRoleId())){
			throw new RuntimeException("角色id为空");
		}
		if (ObjectUtil.isAllEmpty(sysRoleMenuPO.getMenuId(),sysRoleMenuPO.getMenuIdList())){
			throw new RuntimeException("菜单id为空");
		}
		if (ObjectUtil.isEmpty(sysRoleMenuPO.getMenuIdList())){
			LambdaQueryWrapper<SysRoleMenu> existQueryWrapper = new LambdaQueryWrapper<>();
			existQueryWrapper.eq(SysRoleMenu::getRoleId, sysRoleMenuPO.getRoleId());
			existQueryWrapper.eq(SysRoleMenu::getMenuId, sysRoleMenuPO.getMenuId());
			if (this.baseMapper.selectCount(existQueryWrapper) == 0){
				SysRoleMenu sysRoleMenu = BeanUtil.copyProperties(sysRoleMenuPO, SysRoleMenu.class);
				this.save(sysRoleMenu);
			}
		}else{
			LambdaUpdateWrapper<SysRoleMenu> removeWrapper = new LambdaUpdateWrapper<>();
			removeWrapper.eq(SysRoleMenu::getRoleId, sysRoleMenuPO.getRoleId());
			this.remove(removeWrapper);

			for (Integer i : sysRoleMenuPO.getMenuIdList()) {
				SysRoleMenu sysRoleMenu = new SysRoleMenu();
				sysRoleMenu.setRoleId(sysRoleMenuPO.getRoleId());
				sysRoleMenu.setMenuId(i);
				this.save(sysRoleMenu);
			}
		}

		return "操作成功";
	}

	/**
     * @describe 通过id删除角色菜单表
     * @author  zhuhui
     * @date  2024-11-26 22:58:21
     */
	@Override
	public String removeSysRoleMenu(SysRoleMenuPO sysRoleMenuPO) {
		if (ObjectUtil.isAllEmpty(sysRoleMenuPO.getMenuId(),sysRoleMenuPO.getRoleId(),sysRoleMenuPO.getMenuIdList(),sysRoleMenuPO.getRoleIdList())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<SysRoleMenu> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.eq(ObjectUtil.isNotEmpty(sysRoleMenuPO.getRoleId()),SysRoleMenu::getRoleId, sysRoleMenuPO.getRoleId());
		removeWrapper.eq(ObjectUtil.isNotEmpty(sysRoleMenuPO.getMenuId()),SysRoleMenu::getMenuId, sysRoleMenuPO.getMenuId());
		removeWrapper.in(ObjectUtil.isNotEmpty(sysRoleMenuPO.getRoleIdList()),SysRoleMenu::getRoleId, sysRoleMenuPO.getRoleIdList());
		removeWrapper.in(ObjectUtil.isNotEmpty(sysRoleMenuPO.getMenuIdList()),SysRoleMenu::getMenuId, sysRoleMenuPO.getMenuIdList());

		this.remove(removeWrapper);
		return "删除成功";
	}
	
}

